<?php
Class admin_supportController Extends baseController {

	public function index() { 
		$supportModel = new support();
		//$current_page = (isset($_REQUEST['page'])) ? $_REQUEST['page'] : 1;
		//$pos = ($current_page -1 ) * __PAGE_SIZE;
		$lstTv = $supportModel->getSupportByWhere('', '');
		//$count = count((array)$hangsanxuatModel->getAllHangSanXuatByWhere('', ''));
		//$pages = ceil($count/__PAGE_SIZE);

		$this->registry->template->lstSupport = $lstTv;
		//$this->registry->template->pages = $pages;
		
		$this->registry->template->show('admin/support_view');
	}
	
	public function edit(){
		if(isset($_REQUEST['support_id'])){
			$support_id = $_REQUEST['support_id'];
			$supportModel = new support();
			$support = $supportModel->getSupportById($support_id);
			if(count($support)>0){
				$this->registry->template->support = $support;	
			}
		}
		$this->registry->template->show('admin/support_edit');	
	}
	
	public function save(){
			
			$support = new support();
			$support->HoTen = ucwords(trim($_POST['thoten']));
			$support->Yahoo = trim($_POST['tyahoo']);
			$support->Skype = trim($_POST['tskype']);
			$support->DienThoai = trim($_POST['tdienthoai']);
			$support->TrangThai = $_POST['rtrangthai'];
			
			if(isset($_REQUEST['support_id'])) $support->id = trim($_GET['support_id']);
			//else $hang->id = trim($_POST['ttendangnhap']);
			
			
			//lay danh sach user de kiem tra ten dang nhap va email xem da co chua
			if(isset($_REQUEST['support_id'])) $lstSupport = $support->getSupportByWhere('id <> "'.$support->id.'"','');
			else $lstSupport = $support->getSupportByWhere('','');
			if(count($lstSupport)>0)
			{
				//kiem tra neu ton tai yahoo do roi thi hien thong bao
				foreach($lstSupport as $s){
					if((strtolower(trim($s->Yahoo)) == strtolower(trim($support->Yahoo)))){
						$this->registry->template->message_user = 'tài khoản Yahoo đã tồn tại.';
						$this->registry->template->support = $support;
						$this->registry->template->show('admin/support_edit');
						return;	
					}
				}
				
				//kiem tra neu ton tai skype do roi thi hien thong bao
				foreach($lstSupport as $s){
					if((strtolower(trim($s->Skype)) == strtolower(trim($support->Skype)))){
						$this->registry->template->message_user = 'tài khoản Skype đã tồn tại.';
						$this->registry->template->support = $support;
						$this->registry->template->show('admin/support_edit');
						return;	
					}
				}
			}
			if(isset($_REQUEST['support_id'])){
				//$hang->id = $_GET['support_id'];
				$rs = $support->update($support);
				
			}
			else{
				
				$rs = $support->insert($support);
			}
			
			if($rs){
				echo '<script>alert("thành công.");</script>';
				header('location:'.__URL_BASE.'index.php?rt=admin_support');
				exit;
			}
			else{
				echo '<script>alert("Có lỗi xảy ra.");</script>';
			}
		}
		
		public function delete(){
			if(isset($_REQUEST['support_id'])){
				$supportModel = new support();
				$supportModel->delete($_REQUEST['support_id']);
				header('location:'.__URL_BASE.'index.php?rt=admin_support');
				exit;
			}
		}

}
?>
